/**
 * GetTransaction.java
 *
 * Created on March 28, 2007, 4:36 PM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.sql.Connection;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * This form gets the trasaction
 * @author byu
 */
public class GetTransaction extends javax.swing.JFrame {
    
    //tells if if they want the line after this or to display the transaction
    boolean line;
    
    //list of transactions
    List li;
    
    private static GetTransaction instance = null;
    
    
    /**
     * gets a single of this class
     */
    public static synchronized GetTransaction getInstance() {
        
        if (instance == null) {
            instance = new GetTransaction();
        }
        
        return instance;
    }
    /** Creates new form GetTransaction */
    private GetTransaction() {
        initComponents();
        
    }
    /**
     *This method takes in the customer business objects and also sets visible the form
     */
    public void populateList(CustomerBO cust){
        
        try{
            // retrieve a database connection from the pool
            cmbTransactions.removeAllItems();
            
            
            li = TransactionDAO.getInstance().readByCustomer(cust.getId());
            for(int i = 0; i < li.size(); i++){
                TransactionBO tempTrans = (TransactionBO)li.get(i);
                String stgLine = tempTrans.getDate() + "   " + tempTrans.getStatus() + "   "  + tempTrans.getType();
                cmbTransactions.addItem(stgLine);
                
                
            }
            this.setVisible(true);
        
        }catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null,ex.toString(),"Can not get transaction", JOptionPane.ERROR_MESSAGE);
            
        }
        
    }
    
    /**
     * gets the line
     * @param cust 
     */
    public void getLine(CustomerBO cust){
        line = true;
        populateList(cust);
        this.setVisible(true);
    }
    
    /**
     * gets the transaction after passing the customer business objects.
     * @param cust 
     */
    public void getTransaction(CustomerBO cust){
        line = false;
        populateList(cust);
        this.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbTransactions = new javax.swing.JComboBox();
        btnGetList = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        btnGetID = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.BorderLayout(20, 0));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Select Transaction");
        jPanel1.add(jLabel1);

        cmbTransactions.setPreferredSize(new java.awt.Dimension(250, 20));
        jPanel1.add(cmbTransactions);

        btnGetList.setText("Get From List");
        btnGetList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGetListActionPerformed(evt);
            }
        });

        jPanel1.add(btnGetList);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel2.setText("Transaction ID");
        jPanel2.add(jLabel2);

        jTextField1.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel2.add(jTextField1);

        btnGetID.setText("Get by ID");
        jPanel2.add(btnGetID);

        getContentPane().add(jPanel2, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGetListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetListActionPerformed

        TransactionBO trans = (TransactionBO)li.get(cmbTransactions.getSelectedIndex());
        if(line == false){
            NewOrder.getInstance().loadForm(trans);
            this.dispose();
        }else{
            PickTransLineGUI.getInstance().selectProduct(trans);
            this.dispose();
        }
    }//GEN-LAST:event_btnGetListActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GetTransaction().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGetID;
    private javax.swing.JButton btnGetList;
    private javax.swing.JComboBox cmbTransactions;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
